package com.imooc.distributedemo.wallet;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.DefaultBlockParameterNumber;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.core.methods.response.EthBlock;
import org.web3j.protocol.core.methods.response.EthGetBalance;
import org.web3j.protocol.http.HttpService;

import java.math.BigInteger;


// 调用eth-rpc
@Slf4j
public class EthRpcCallSolution {
    private static final String MY_ETH_ADDRESS = "0x26e598f4843c106a5bb6da553549dc7e2afee016";
    private Web3j web3j;

    public EthRpcCallSolution(Web3j web3j){
        this.web3j = web3j;//Web3j.build(new HttpService("https://sepolia.infura.io/v3/753af77cda5240b9a9ea1b28cf192002"));
    }

    public static void main(String[] args) throws Exception{

        Web3j web3j = Web3j.build(new HttpService("https://sepolia.infura.io/v3/753af77cda5240b9a9ea1b28cf192002"));
        EthRpcCallSolution ethCall = new EthRpcCallSolution(web3j);
//        long pollInterval = 12000; // 12秒轮询一次
//        long targetBlocks = 15; // 目标区块数量
//        BlockHeightFetch poller2 = new BlockHeightFetch(web3j, pollInterval, targetBlocks);
//        poller2.start();

//        查询这条交易记录   https://sepolia.etherscan.io/tx/0xcec3a16f75e6daad191b6124c5839c546be60c0c2d734ad8481efacdfdeba92a
//        区块高度 https://sepolia.etherscan.io/block/6798057
//        transaction-hash 0xcec3a16f75e6daad191b6124c5839c546be60c0c2d734ad8481efacdfdeba92a
//        0x631E9B031b16b18172a2B9D66C3668A68a668d20-->0.1ETH-->0x26E598f4843C106a5bB6DA553549DC7E2Afee016
//        BigInteger targetBlockNumber = new BigInteger("6798057");
//        String ethBlockJsonStr = ethCall.hanldeEthGetBlockByNumber(targetBlockNumber);
//        System.out.println("获取的区块信息:"+ethBlockJsonStr);

        EthGetBalance ethGetBalance = web3j.ethGetBalance(MY_ETH_ADDRESS, DefaultBlockParameterName.LATEST).sendAsync().get();
        BigInteger myBalance = ethGetBalance.getBalance();
        System.out.println("我的eth地址余额是:"+myBalance.intValue());
        String hexNumber = Integer.toHexString(myBalance.intValue());
        System.out.println(hexNumber);
        // 1569325056   {'jsonrpc': '2.0', 'id': 1, 'result': '0x16345785d8a0000'}

    }


    /**
     * eth_getBlockByNumber -- 通过区块高度获取区块数据信息
     * @param lastKnownBlockNumber
     */
    private String hanldeEthGetBlockByNumber(BigInteger lastKnownBlockNumber) {
        String ethBlockJsonStr="";
        try {
            DefaultBlockParameter parameter = new DefaultBlockParameterNumber(lastKnownBlockNumber);
            EthBlock ethBlock = web3j.ethGetBlockByNumber(parameter, true).sendAsync().get();
            ethBlockJsonStr = JSON.toJSONString(ethBlock);
            log.info("解析的ethBlock数据是:{}",ethBlockJsonStr);
        }catch (Exception e){
            log.error("通过区块num解析区块数据异常,lastKnownBlockNumber:{}",lastKnownBlockNumber,e);
        }
        return ethBlockJsonStr;
    }

}
